<template>
    <div class="app-container">
        <el-page-header @back="goBack" >
        </el-page-header>
      <el-table
        v-loading="loading"
        border
        :data="inventorydata"
        style="margin-top: 20px"
        :summary-method="getSummaries"
        show-summary
      >
        <el-table-column label="序号" width="60" type="index" align="center">
        </el-table-column>
        <el-table-column
          label="状态"
          align="center"
          prop="status"
          :show-overflow-tooltip="true"
        >
          <template #default="scope">
            <dict-tag
              :options="ems_inventorysheet_status"
              :value="scope.row.status"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="盘点单编码"
          width="200"
          align="center"
          prop="inventoryNum"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
  
        <el-table-column
          label="盘点月份"
           width="100"
          align="center"
          prop="inventoryMonth"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="盘点仓库"
          align="center"
          width="180"
          prop="machineName"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          label="盘点人"
          width="100"
          align="center"
          prop="inventoryUser"
          :show-overflow-tooltip="true"
        />
        <el-table-column
                prop="code"
                label="纸张编码"
                min-width="180"
                align="center"
              ></el-table-column>
              <el-table-column
                prop="name"
                min-width="200"
                label="纸张名称"
                align="center"
              ></el-table-column>
              <el-table-column prop="specs" label="规格"  min-width="120" align="center">
                <template #default="scope">
                  <dict-tag
                    :options="mes_paper_specs"
                    :value="scope.row.specs"
                  />
                </template>
              </el-table-column>
        <el-table-column
                prop="machineNumber"
                label="库存数量"
                width="100"
                align="center"
              ></el-table-column>
              <el-table-column
                prop="inventoryNumber"
                label="盘点数量"
                width="100"
                align="center"
              ></el-table-column>
              <el-table-column
                prop="profitLoss"
                label="盘盈/盘亏"
                width="100"
                align="center"
              ></el-table-column>
              <el-table-column
                prop="reason"
                label="差额原因"
                width="120"
                align="center"
              ></el-table-column>
        <el-table-column
                prop="material"
                label="纸张类型"
                width="100"
                align="center"
              >
                <template #default="scope">
                  <dict-tag
                    :options="mes_paper_material"
                    :value="scope.row.material"
                  />
                </template>
              </el-table-column>
             
              <el-table-column prop="brand" label="品牌" align="center">
                <template #default="scope">
                  <dict-tag
                    :options="mes_paper_brand"
                    :value="scope.row.brand"
                  />
                </template>
              </el-table-column>
              <el-table-column
                prop="weight"
                label="定量"
                align="center"
              ></el-table-column>
              <el-table-column prop="unit" label="单位" align="center">
                <template #default="scope">
                  <dict-tag :options="mes_paper_unit" :value="scope.row.unit" />
                </template>
              </el-table-column>
             
              <el-table-column
          label="创建时间"
          align="center"
          prop="confirmDate"
          width="100"
          :show-overflow-tooltip="true"
        />
      </el-table>
    </div>
  </template>
   
  <script setup name="supplier">
  import { downLoadBlob } from "@/utils/ruoyi";
  import useTagsViewStore from "@/store/modules/tagsView";
  const tagsViewStore = useTagsViewStore();
  import {
    inventoryDetailList,
  } from "@/api/storage/inventorySheet.js";
  const { proxy } = getCurrentInstance();
  const { ems_inventorysheet_status,mes_paper_specs,mes_paper_brand,mes_paper_unit,mes_paper_material } = proxy.useDict(
    "ems_inventorysheet_status","mes_paper_specs","mes_paper_brand","mes_paper_unit","mes_paper_material"
  );
  const router = useRouter();
  const route = useRoute();
  const inventorydata = ref([]);
  const loading = ref(false);
  const total = ref(0);
  const profitLossSumTotal=ref(0);
  import useUserStore from "@/store/modules/user";
  const userInfo = useUserStore();
  import { ElMessage, ElMessageBox } from "element-plus";
  const showSearch = ref(true);
  const queryParams = ref({
    pageNum: 1,
    pageSize: 10,
  });
  const inventory_store = [
    { label: "仓库", type: "0" },
    { label: "机台", type: "1" },
  ];
  

 
  function getCurrentTime() {
    const now = new Date();
    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, "0");
    const day = String(now.getDate()).padStart(2, "0");
    const hours = String(now.getHours()).padStart(2, "0");
    const minutes = String(now.getMinutes()).padStart(2, "0");
    const seconds = String(now.getSeconds()).padStart(2, "0");
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  }

  // 合计
const getSummaries = (param) => {
  const { columns, data } = param;
  const sums = [];
  columns.forEach((column, index) => {
    if (index === 0) {
      sums[index] = "合计";
      return;
    }
    switch (column.property) {
      case "profitLoss":
        sums[index] = profitLossSumTotal.value;
        break;
      default:
        break;
    }
  });
  return sums;
};

  function getList() {
    loading.value = true;
    if(route.query?.paperId){
      queryParams.value.paperId=route.query.paperId;
      queryParams.value.machineId=route.query.machineId;
      queryParams.value.startDate=route.query.startDate;
      queryParams.value.endDate=route.query.endDate;
    }
    inventoryDetailList(queryParams.value)
      .then((response) => {
        inventorydata.value = response.rows;
        if (response.rows.length > 0) {
        profitLossSumTotal.value =response.rows[0].profitLossSum;
      }
        total.value = response.total;
        loading.value = false;
      })
      .catch((err) => {
        loading.value = false;
      });
  }
  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }
  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
  }
  getList();

    //  返回
    const goBack = () => {
    setTimeout(() => {
    router.go(-1);
    tagsViewStore.delView(route);
  }, 0);
  };
  </script>
  